package com.zcxy360.sandtable.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zcxy360.sandtable.domain.User;
import com.zcxy360.sandtable.domain.Video;
import com.zcxy360.sandtable.domain.VideoExample;
import com.zcxy360.sandtable.domain.VideoExample.Criteria;
import com.zcxy360.sandtable.domain.VideoMapper;
import com.zcxy360.sandtable.domain.VvideoMapper;
import com.zcxy360.sandtable.util.JavacvUtil;
import com.zcxy360.sandtable.util.ShiroUtil;
import com.zcxy360.sandtable.util.StringUtil;
import com.zcxy360.sandtable.util.UploadUtil;

@Transactional
@Service
public class VideoService {

	@Autowired
	private VideoMapper videoMapper;
	@Autowired
	private VvideoMapper vvideoMapper;

	@Value("${res.server}")
	private String resServer;

	public Video getByVideoId(Integer videoId,Integer deep) {
		Video video = null;
		switch (deep) {
		case 0:
			video = videoMapper.selectByPrimaryKey(videoId);
			break;
		case 1:
			video = videoMapper.selectByPrimaryKeyWithExtra(videoId);
			break;
		default:
			break;
		}
		return video;
	}
	
	public Object list(Video video, String key,String orderByClause,Integer sign,Integer pageNum, Integer pageSize, Integer navigatePages) {
		User loginUser = ShiroUtil.getUser();
		String loginRole = loginUser.getRole();
		if(!loginRole.equals("admin")) {
			video.setOrgId(loginUser.getOrgId());
		}
		if(sign != null) {
			//只看自己
			if(sign == 1 && !loginRole.equals("admin")) {
				video.setUserId(loginUser.getUserId());
			}
			//看非自己的其他老师资源
			if(sign == 2) {
				video.setUserId(loginUser.getUserId());
			}
			//看内建资源
			if(sign == 3) {
				video.setUserId(1);
				video.setOrgId(null);
			}
			//看本机构自己的所有资源及其它老师共享资源及内建资源
			if(sign == 4) {
				video.setUserId(loginUser.getUserId());
			}
			//看本机构所有资源及内建资源
			if(sign == 5) {
			}
		}
		PageHelper.startPage(pageNum, pageSize);
		List<Video> videos = videoMapper.listWithExtra(video,StringUtil.wrapper(key),orderByClause,sign);
		return new PageInfo<>(videos, navigatePages);
	}
	
	
	public Video post(MultipartFile file,Video video) {
		User loginUser = ShiroUtil.getUser();
		video.setUserId(loginUser.getUserId());
		video.setOrgId(loginUser.getOrgId());
		resolveVideo(file,video);
		videoMapper.insertSelective(video);
		vvideoMapper.insertByVideoId(video.getVideoId());
		return video;
	}

	public boolean update(MultipartFile file,Video video) {
		User loginUser = ShiroUtil.getUser();
		String loginRole = loginUser.getRole();
		if(!loginRole.equals("admin")) {
			Video v = videoMapper.selectByPrimaryKey(video.getVideoId());
			if(loginRole.equals("teacher")) {
				if(!v.getUserId().equals(loginUser.getUserId())) {
					throw new RuntimeException("老师只能修改本人的资源");
				}
			}
			if(loginRole.equals("manager")) {
				if(!v.getOrgId().equals(loginUser.getOrgId())) {
					throw new RuntimeException("机构管理员只能修改本机构的资源");
				}
			}
		}
		if (file != null) {
			resolveVideo(file,video);
		}
		videoMapper.updateByPrimaryKeySelective(video);
		vvideoMapper.insertByVideoId(video.getVideoId());
		return true;
	}

	public boolean delete(int[] videoIds) {
		User loginUser = ShiroUtil.getUser();
		String loginRole = loginUser.getRole();
		Video v;
		if(!loginRole.equals("admin")) {
			for(int videoId:videoIds) {
				v = videoMapper.selectByPrimaryKey(videoId);
				if(loginRole.equals("teacher")) {
					if(!v.getUserId().equals(loginUser.getUserId())) {
						throw new RuntimeException("老师只能删除本人的资源");
					}
				}
				if(loginRole.equals("manager")) {
					if(!v.getOrgId().equals(loginUser.getOrgId())) {
						throw new RuntimeException("机构管理员只能删除本机构的资源");
					}
				}
			}
		}
		videoMapper.deleteBatch(videoIds);
		return true;
	}

	public boolean checkName(String name) {
		User loginUser = ShiroUtil.getUser();
		VideoExample ex = new VideoExample();
		Criteria or = ex.or();
		or.andNameEqualTo(name);
		if(loginUser.getOrgId() != null) {
			or.andOrgIdEqualTo(loginUser.getOrgId());
		}
		return videoMapper.countByExample(ex) == 0;
	}
	
	private void resolveVideo(MultipartFile file,Video video) {
		if(file == null) {
			throw new RuntimeException("请上传文件");
		}
		String path = UploadUtil.uploadVideo(file);
		video.setPath(path.replace("E:/res", resServer));
		video.setSize((int) file.getSize() / (1024 * 1024));
		Map<String, Object> infoMap = JavacvUtil.generateThumbnailAndGetInfo(path);
		video.setThumbnailPath(((String) infoMap.get("thumbnailPath")).replace("E:/res", resServer));
		video.setDuration(((Long) infoMap.get("duration")).intValue());
		video.setRoughTime((int)Math.ceil(video.getDuration()/60d));
		video.setResolution((String) infoMap.get("resolution"));
	}
	
}
